package com.iheima.reggie_take.controller;

/**
 * Created with IntelliJ IDEA.
 * ClassName: DishController
 * Package: com.iheima.reggie_take.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-01-17
 * Time: 20:43
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iheima.reggie_take.common.R;
import com.iheima.reggie_take.dto.DishDto;
import com.iheima.reggie_take.entity.Category;
import com.iheima.reggie_take.entity.Dish;
import com.iheima.reggie_take.service.CategoryService;
import com.iheima.reggie_take.service.DishFlavorService;
import com.iheima.reggie_take.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;  //菜品

    @Autowired
    private DishFlavorService dishFlavorService;  //口味

    @Autowired
    private CategoryService categoryService;

    //新增菜品 /dish
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        //这里添加涉及到两张表 1.菜品表插入数据 2.口味表也要插入数据
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        log.info(dishDto.toString());
        return R.success("新增菜品成功!");
    }


    //菜品信息的分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        //返回的数据 没有菜品分类 但是DishDto中有 那么将Dish的拷贝到DishDto中
        Page<DishDto> dishDtoPage = new Page<>();


        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行分页条件
        dishService.page(pageInfo,queryWrapper);

        //对象拷贝 最后一个参数是忽略掉的 不拷贝的
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();

        List<DishDto> list = records.stream().map((item) ->{
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId(); //分类id
            //通过id查到分类对象 目的是查到分类的名称
            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }

            return dishDto;

        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);


        return R.success(dishDtoPage);
    }

    /**
     * 根据id来查询菜品信息 和 对应的口味信息 回显服务
     * @param id
     * @return
     */
    //修改菜品 GET /dish/1397850851245600769
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }

    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        //更新也是两张表

        dishService.updateWithFlavor(dishDto);

        return R.success("新增菜品成功!");

    }


    /**
     * 更新菜品为停售
     * @param ids Dish的id
     * @return
     */
    @PostMapping("/status/0")
    public R<String> updateStatusStop(Long ids){
        Dish dish=dishService.getById(ids);
        dish.setStatus(0);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getId, ids);
        dishService.update(dish, lambdaQueryWrapper);
        return R.success("更新成功");
    }


    /**
     * 更新菜品状态为起售
     * @param ids Dish的id
     * @return
     */
    @PostMapping("/status/1")
    public R<String> updateStatusStart(Long ids){
        Dish dish=dishService.getById(ids);
        dish.setStatus(1);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getId, ids);
        dishService.update(dish, lambdaQueryWrapper);
        return R.success("更新成功");
    }

    /**
     * 这里是逻辑删除，不是真删，把isDeleted字段更新为1就不显示了，间接完成了逻辑删除
     * @param ids Dish的id
     * @return
     */
    @DeleteMapping()
    public R<String> deleteDish(Long ids){
        Dish dish=dishService.getById(ids);
        dish.setIsDeleted(1);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getId, ids);
        dishService.update(dish, lambdaQueryWrapper);
        return R.success("删除成功");
    }


    // 套餐管理 添加套餐的添加菜品
    //dish/list?categoryId=1615245763949764609
    @GetMapping("list")
    public R<List<Dish>> list(Dish dish){

        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());

        //添加条件 查询状态为1的数据 (起售状态)
        queryWrapper.eq(Dish::getStatus,1);

        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);

        return R.success(list);
    }


}
